﻿using AutoMapper;

using Azure;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Models;
using ZFB.Repository.Community;
using ZFB.Repository.Extensions;
using ZFB.Repository.HouseAppointments;

namespace ZFB.Services
{
    public class HouseAppointmentsServices : IHouseAppointmentsServices
    {
        private readonly IHouseAppointmentsRepository _houseAppointmentsRepository;
        private readonly IMapper mapper;

        public HouseAppointmentsServices(IHouseAppointmentsRepository houseAppointmentsRepository, IMapper mapper)
        {
            _houseAppointmentsRepository = houseAppointmentsRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> AddNewAsync(long? userId, string name, string phoneNum, long houseId, DateTime visitDate)
        {
            var app = new T_HouseAppointments
            {
                UserId = userId,
                Name = name,
                PhoneNum = phoneNum,
                HouseId = houseId,
                VisitDate = visitDate,
                Status = "未处理"
            };
            var res = await _houseAppointmentsRepository.AddAsync(app);
            return res <= 0 ? new ApiResult("预约失败！") : new ApiResult(app.Id);
        }

        public Task<ApiResult> Follow(long adminUserId, long huoseAppointmentId)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResult> GetByIdAsync(long id)
        {
            var data = await _houseAppointmentsRepository.GetListAsync(e => e.Id == id);
            if (data == null)
            {
                return new ApiResult("获取失败!");
            }
            else
            {
                var dto = mapper.Map<List<HouseAppointmentsDTO>>(data);
                return new ApiResult(dto);
            }
        }

        public async Task<ApiResult> GetPageDateAsync(long cityId, string status, int page, int limit)
        {
            var data = await _houseAppointmentsRepository.GetPagesAsync(page,limit,e=>e.T_Houses.T_Communities.T_Regions.CityId==cityId&&e.Status==status);
            if (data == null)
            {
                return new ApiResult("获取失败!");
            }
            else
            {
                var dto = mapper.Map<List<HouseAppointmentsDTO>>(data.Items);
                var pagedata = new Repository.Extensions.Page<HouseAppointmentsDTO>();
                pagedata.Items = dto;
                pagedata.TotalItems=data.TotalItems;
                pagedata.TotalPages=data.TotalPages;
                pagedata.ItemsPerPage=data.ItemsPerPage;
                pagedata.ItemsPerPage = limit;
                pagedata.CurrentPage = page;
                return new ApiResult(pagedata);
            }
        }

        public async Task<ApiResult> GetToTalCountAsync(long cityId, string status)
        {
           var count=await _houseAppointmentsRepository.LongCountAsync (e => e.T_Houses.T_Communities.T_Regions.CityId == cityId && e.Status == status);
          return new ApiResult(count);
        }
    }
}
